﻿namespace BEGDC.CC.Web.EZVisit.Common
{
    #region Using Directives

    using System;
    using System.Data;
    using BL.Common;
    using UserControls;
    using Workflow;

    #endregion

    public partial class EZVRequestCustomer : BasePage
    {
        internal const string SessionKey = "EZVRequestCustomer_";

        private string ApplicationSessionKey
        {
            get
            {
                return this.Request.QueryString["sn"];
            }
        }

        protected int RowID
        {
            get
            {
                return Convert.ToInt32(this.Request["CustomerId"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                WebUtility.BuildDropDown(this.ddlResponsibility, DicType.RESPONSIBILITY, false, false);

                this.InitForm();
            }
        }

        private void InitForm()
        {
            if (this.RowID < 1)
            {
                return;
            }

            var dt = this.Session[SessionKey + this.ApplicationSessionKey] as DataTable;

            if (dt == null)
            {
                return;
            }

            DataRow row = dt.Rows[this.RowID - 1];

            this.tbCustomerName.Text = row["NameCN"].ToString();
            this.tbCustomerNamePY.Text = row["NameEN"].ToString();
            this.tbTitle.Text = row["TitleCN"].ToString();
            this.tbTitleEN.Text = row["TitleEN"].ToString();
            this.tbCompany.Text = row["CompanyCN"].ToString();
            this.tbCompanyEN.Text = row["CompanyEN"].ToString();
            this.ddlResponsibility.SelectedValue = row["Responsibility"].ToString();
            this.tbRemark.Text = row["Remark"].ToString();
            this.rblGenders.SelectedValue = Convert.ToInt32(row["Gender"]).ToString();
            this.tbIsExist.Text = row["IsExisted"].ToString();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.Page.IsValid && this.UpdateCustomerSession())
            {
                this.Response.Write(@"<script type=""text/javascript"">window.close();window.returnValue='reload';</script>");
            }
        }

        private bool UpdateCustomerSession()
        {
            if (this.RowID < 0)
            {
                return false;
            }

            var dt = this.Session[SessionKey + this.ApplicationSessionKey] as DataTable;

            if (dt == null)
            {
                return false;
            }

            string condition = string.Format("RowId <>{0} and IsEnabled=1 and NameCN='{1}' and CompanyCN='{2}' and TitleCN='{3}' and Gender={4}", this.RowID, this.tbCustomerName.Text.Trim(), this.tbCompany.Text.Trim(), this.tbTitle.Text.Trim(), this.rblGenders.SelectedValue);
            if (dt.Select(condition).Length > 0)
            {
                this.Response.Write(@"<script type=""text/javascript"">alert('This person is already in the list!')</script>");
                return false;
            }

            this.tbIsExist.Text = CISFlag.NotExisted;

            DataRow row;
            if (this.RowID == 0)
            {
                row = dt.NewRow();

                row["NameCN"] = this.tbCustomerName.Text.Trim();
                row["NameEN"] = this.tbCustomerNamePY.Text.Trim();
                row["CompanyCN"] = this.tbCompany.Text.Trim();
                row["CompanyEN"] = this.tbCompanyEN.Text.Trim();
                row["TitleCN"] = this.tbTitle.Text.Trim();
                row["TitleEN"] = this.tbTitleEN.Text.Trim();
                row["Responsibility"] = this.ddlResponsibility.SelectedValue;
                row["ResponsibilityText"] = this.ddlResponsibility.SelectedItem.Text;
                row["Gender"] = this.rblGenders.SelectedValue.Equals("1");
                row["Remark"] = this.tbRemark.Text;
                row["IsExisted"] = this.tbIsExist.Text;
                row["CreateAt"] = DateTime.Now;
                row["CreateBy"] = this.UserInfo.NameEN;
                row["IsEnabled"] = true;

                dt.Rows.Add(row);
            }
            else if (this.RowID > 0)
            {
                row = dt.Rows[this.RowID - 1];
                row["NameCN"] = this.tbCustomerName.Text.Trim();
                row["NameEN"] = this.tbCustomerNamePY.Text.Trim();
                row["CompanyCN"] = this.tbCompany.Text.Trim();
                row["CompanyEN"] = this.tbCompanyEN.Text.Trim();
                row["TitleCN"] = this.tbTitle.Text.Trim();
                row["TitleEN"] = this.tbTitleEN.Text.Trim();
                row["Responsibility"] = this.ddlResponsibility.SelectedValue;
                row["ResponsibilityText"] = this.ddlResponsibility.SelectedItem.Text;
                row["Gender"] = this.rblGenders.SelectedValue.Equals("1");
                row["Remark"] = this.tbRemark.Text;
                row["IsExisted"] = this.tbIsExist.Text;
                row["UpdateAt"] = DateTime.Now;
                row["UpdateBy"] = this.UserInfo.NameEN;
                row["IsEnabled"] = true;
            }

            this.Session[SessionKey + this.ApplicationSessionKey] = SetRowID(dt);

            return true;
        }

        private static DataTable SetRowID(DataTable table)
        {
            if (table != null && table.Rows.Count > 0)
            {
                for (int i = 1; i <= table.Rows.Count; i++)
                {
                    table.Rows[i - 1]["RowId"] = i.ToString();
                }
            }

            return table;
        }
    }
}